package com.example.chat.controller;

import com.example.chat.domain.dto.GroupInviteDTO;
import com.example.chat.domain.dto.GroupInviteYON;
import com.example.chat.domain.dto.PageQueryDTO;
import com.example.chat.domain.dto.CreateGroupFormDTO;
import com.example.chat.domain.entity.GroupInvite;
import com.example.chat.result.Result;
import com.example.chat.service.GroupChatService;
import com.example.chat.utils.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/GroupChats")
@CrossOrigin
@Slf4j
@Tag(name = "群聊相关接口")
public class GroupChatController {

    @Resource
    private GroupChatService groupChatService;

    @Operation(summary = "创建群聊")
    @PostMapping("/add")
    public Result createGroupChat(@RequestBody CreateGroupFormDTO createGroupFormDTO){
        log.info("ID为{}的用户创建群聊，群聊名为{}", UserHolder.getCurrentId(),createGroupFormDTO.getGroupName());
        return groupChatService.createGroupChat(createGroupFormDTO.getIds(),createGroupFormDTO.getGroupName());
    }

    @Operation(summary = "查询个人群聊")
    @GetMapping
    public Result queryGroupChats(){
        return groupChatService.queryGroupChats();
    }

    @Operation(summary = "查询群聊列表")
    @GetMapping("/grouplist")
    public Result queryGroupList(){
        return groupChatService.queryGroupList();
    }

    @Operation(summary = "查询群聊消息")
    @PostMapping("/content")
    public Result queryGroupContents(@RequestBody PageQueryDTO queryContentDTO){
        return groupChatService
                .queryGroupContents(queryContentDTO.getId(),queryContentDTO.getTimestamp(),queryContentDTO.getOffset());
    }

    @Operation(summary = "清空群聊未读消息")
    @PostMapping("/clear/{type}/{id}")
    public Result tipClearOrReduce(@PathVariable Long id,@PathVariable Integer type){
        return groupChatService.tipClearOrReduce(id,type);
    }

    @Operation(summary = "查询群聊信息")
    @GetMapping("/GroupInfo/{id}")
    public Result getGroupInfo(@PathVariable Long id){
        return groupChatService.getGroupInfo(id);
    }

    @Operation(summary = "根据id删除群聊列表")
    @DeleteMapping("/list/{id}")
    public Result deleteChatListById(@PathVariable Long id){
        return groupChatService.deleteChatListById(id);
    }

    @Operation(summary = "查询群聊完整信息")
    @GetMapping("/{id}")
    public Result queryGroupFulInfo(@PathVariable Long id){
        return groupChatService.queryGroupFulInfo(id);
    }

    @Operation(summary = "解散群聊")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        log.info("ID为{}的用户解散了群聊{}", UserHolder.getCurrentId(),id);
        return groupChatService.deleteById(id);
    }

    @Operation(summary = "用户离开群聊")
    @DeleteMapping("/leave/{id}")
    public Result leaveById(@PathVariable Long id){
        log.info("ID为{}的用户离开了群聊{}", UserHolder.getCurrentId(),id);
        return groupChatService.leaveById(id);
    }

    @Operation(summary = "邀请新人加群")
    @PostMapping("/invite")
    public Result inviteUser(@RequestBody GroupInviteDTO groupInviteDTO){
        log.info("ID为{}的用户邀请{}进入群聊{}", UserHolder.getCurrentId()
                ,groupInviteDTO.getInvitees(),groupInviteDTO.getGroupId());
        return groupChatService.inviteUser(groupInviteDTO);
    }

    @Operation(summary = "查询已被邀请人")
    @GetMapping("/invite")
    public Result getInvitees(@RequestParam Long groupId){
        return groupChatService.getInvitees(groupId);
    }

    @Operation(summary = "群邀请同意或拒绝")
    @PutMapping("/invite")
    public Result inviteByType(@RequestBody GroupInviteYON groupInviteYON){
        String s = groupInviteYON.getType() == 1 ? "同意" : "拒绝";
        log.info("ID为{}的用户{}进入群聊{}", UserHolder.getCurrentId(),s,groupInviteYON.getGroupId());
        return groupChatService.inviteByType(groupInviteYON);
    }

    @Operation(summary = "查询群邀请")
    @GetMapping("/invite/detail")
    public Result getListInvite(){
        return groupChatService.getListInvite();
    }
}
